package org.pulloid.android.sample;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import org.pulloid.android.AndroidCursorDef;
import org.pulloid.android.PulloidAdapter;

import android.app.ListActivity;
import android.os.Bundle;
import android.widget.Toast;

public class RSSActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.rss_feeds_list);
        
		PulloidAdapter adapter = new PulloidAdapter(this, R.layout.rss_feed_item);
		AndroidCursorDef def = adapter.createRootContext().selectElement("rss").selectElement("channel").selectElement("item").defineCursor();
		def.getContext().selectElement("title").createField(R.id.title);
		def.getContext().selectElement("media:content").createFieldOnAttribute(R.id.image, "url");
		def.getContext().selectElement("media:description").createField(R.id.description);
		def.getContext().selectElement("pubDate").createField(R.id.date);

		try {
			InputStream in = new URL("http://feeds.nytimes.com/nyt/rss/HomePage").openStream();
			def.pull(in);
		} catch (IOException e) {
			Toast.makeText(this, "Unable to load photos :(", Toast.LENGTH_LONG);
		}

        setListAdapter(adapter);
    }
}
